//
//  JianLiViewController.m
//  HuaLang
//
//  Created by iking on 2018/3/9.
//  Copyright © 2018年 iking. All rights reserved.
//

#import "WuLiuInfoViewController.h"
#import "WuLiuItemTableViewCell.h"
#import "WuLiuProgressTableViewCell.h"

@interface WuLiuInfoViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray *wuLiuDataArray;

@end

@implementation WuLiuInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = UIColorWithRGB(242, 242, 242);
    [self loadBaseTitleLable:@"物流信息"];
    [self.view addSubview:self.tableView];
    [self getWuLiuData];
}
#pragma mark - 获取物流信息
- (void)getWuLiuData
{
    if (self.ordersnumber) {
        NSString *code = [NSString stringWithFormat:@"%@%@",safeCode,[GetTimestamp getNowTimeTimestamp]];
        [MBProgressHUD showMessage:nil toView:self.view];
        [TFNetworking getWithURLString:[NSString stringWithFormat:@"%@/api/json/member/order.ashx?action=getexpress&companyid=%@&code=%@&timestamp=%@&ordersnumber=%@",kServerURL,companyId,[MD5Encrypt  MD5ForLower32Bate:code],[GetTimestamp getNowTimeTimestamp],self.ordersnumber] parameters:nil success:^(id responseObject) {
            [MBProgressHUD hideHUDForView:self.view];
            
            self.wuLiuDataArray = [NSArray array];
            if ([responseObject[@"Success"] intValue] == 1) {
                if (responseObject[@"Traces"] && [responseObject[@"Traces"] isKindOfClass:[NSArray class]]) {
                    NSArray *array = [NSArray arrayWithArray:responseObject[@"Traces"]];
                    self.wuLiuDataArray = [[array reverseObjectEnumerator] allObjects];
                }
                [self.tableView reloadData];
            } else {
                [TFHUD addRemindHUD:self.view message:responseObject[@"returnMsg"]];
            }
            
        } failure:^(NSError *error) {
            NSLog(@"---%@",error);
            [TFHUD addRemindHUD:self.view message:@"网络不给力..."];
        }];
    }
}
#pragma mark - tableView delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 2;
    } else {
        // 物流信息
        return self.wuLiuDataArray.count;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        WuLiuItemTableViewCell *cell = [[WuLiuItemTableViewCell alloc] init];
        if (self.wuLiuInfo) {
            NSArray *array = [self.wuLiuInfo componentsSeparatedByString:@","];
            NSString *str1 = array[0];
            NSArray *gongSiArr = [str1 componentsSeparatedByString:@"|"];
            NSString *str2 = array[1];
            NSArray *numArr = [str2 componentsSeparatedByString:@"|"];
            
            NSArray *array1 = @[@"物流公司",@"运单编号"];
            NSArray *array2 = @[gongSiArr[1],numArr[1]];
            cell.leftLB.text = array1[indexPath.row];
            cell.rightLB.text = array2[indexPath.row];
            cell.line.hidden = YES;
        }
        
        cell.selectionStyle = NO;
        return cell;
    } else {
        WuLiuProgressTableViewCell *cell = [[WuLiuProgressTableViewCell alloc] init];
        if (indexPath.row == 0) {
            cell.pointImg.image = UIImageNamed(@"bluepoint");
            cell.line0.hidden = YES;
        }
        if (self.wuLiuDataArray.count != 0) {
            NSDictionary *dic = self.wuLiuDataArray[indexPath.row];
            cell.label2.text = dic[@"AcceptStation"];
            cell.timeLB.text = dic[@"AcceptTime"];
        }
        cell.selectionStyle = NO;
        return cell;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return rateHeight(75);
    } else {
        return rateHeight(225);
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return rateHeight(20);
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, kScreenWidth, kScreenHeight-64) style:(UITableViewStyleGrouped)];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.backgroundColor = UIColorWithRGB(242, 242, 242);
        _tableView.separatorStyle = NO;
    }
    return _tableView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
